package com.ruoyi.rgpp.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rgpp.mapper.JmrJibencompanyMapper;
import com.ruoyi.rgpp.domain.JmrJibencompany;
import com.ruoyi.rgpp.service.IJmrJibencompanyService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 企业基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-13
 */
@Service
public class JmrJibencompanyServiceImpl implements IJmrJibencompanyService
{
    @Autowired
    private JmrJibencompanyMapper jmrJibencompanyMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询企业基本信息
     *
     * @param id 企业基本信息主键
     * @return 企业基本信息
     */
    @Override
    public JmrJibencompany selectJmrJibencompanyById(Long id)
    {
        return jmrJibencompanyMapper.selectJmrJibencompanyById(id);
    }

    /**
     * 查询企业基本信息列表
     *
     * @param jmrJibencompany 企业基本信息
     * @return 企业基本信息
     */
    @Override
    public List<JmrJibencompany> selectJmrJibencompanyList(JmrJibencompany jmrJibencompany)
    {
        return jmrJibencompanyMapper.selectJmrJibencompanyList(jmrJibencompany);
    }

    /**
     * 新增企业基本信息
     *
     * @param jmrJibencompany 企业基本信息
     * @return 结果
     */
    @Override
    public int insertJmrJibencompany(JmrJibencompany jmrJibencompany)
    {
        return jmrJibencompanyMapper.insertJmrJibencompany(jmrJibencompany);
    }

    /**
     * 修改企业基本信息
     *
     * @param jmrJibencompany 企业基本信息
     * @return 结果
     */
    @Override
    public int updateJmrJibencompany(JmrJibencompany jmrJibencompany)
    {
        return jmrJibencompanyMapper.updateJmrJibencompany(jmrJibencompany);
    }

    /**
     * 批量删除企业基本信息
     *
     * @param ids 需要删除的企业基本信息主键
     * @return 结果
     */
    @Override
    public int deleteJmrJibencompanyByIds(String ids)
    {
        return jmrJibencompanyMapper.deleteJmrJibencompanyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业基本信息信息
     *
     * @param id 企业基本信息主键
     * @return 结果
     */
    @Override
    public int deleteJmrJibencompanyById(Long id)
    {
        return jmrJibencompanyMapper.deleteJmrJibencompanyById(id);
    }

    @Override
    public String importJibencompany(List<JmrJibencompany> JmrJibencompanyList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(JmrJibencompanyList) || JmrJibencompanyList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (JmrJibencompany JmrJibencompany : JmrJibencompanyList)
        {
            try
            {
                // 验证是否存在这个用户
                JmrJibencompany e = jmrJibencompanyMapper.selectJmrJibencompanyById(JmrJibencompany.getId());
                if (StringUtils.isNull(e))
                {
                    BeanValidators.validateWithException(validator, JmrJibencompany);
                    this.insertJmrJibencompany(JmrJibencompany);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入数据 " + JmrJibencompany.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, JmrJibencompany);
                    JmrJibencompany.setUpdateBy(operName);
                    this.updateJmrJibencompany(JmrJibencompany);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入数据 " + JmrJibencompany.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、导入数据 " + JmrJibencompany.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入数据 " + JmrJibencompany.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
